package com.jams.goodox.activity.serve.meet;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;
import com.jams.goodox.activity.ChoosePayActivity;
import com.jams.goodox.activity.serve.ServeOrderBaseDetailActivity;
import com.jams.goodox.constant.Constants;
import com.jams.goodox.entity.MeetOrderDetailBean;
import com.jams.goodox.entity.MeetSessionItemBean;
import com.jams.goodox.entity.PayInfo;
import com.jams.goodox.entity.ServeOrderDetailBean;
import com.jams.goodox.entity.resp.ReservationPlaceResp;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.NoScrollGridView;
import com.jams.goodox.widget.NoScrollListView;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;
import com.jams.library.fresco.FrescoUtil;

import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * 场地预定订单详情页
 * Created by guozhen.hou on 2017/7/11.
 */

public class MeetOrderDetailActivity extends ServeOrderBaseDetailActivity {

    private ServeOrderDetailBean meetOrderDetailBean;

    private boolean isChangePayStatus = false;//是否取消订单
    private String orderId;

    @Override
    protected void getOrderDetail() {
        showLoading();
        if (TextUtils.isEmpty(orderId)) orderId = getIntent().getStringExtra("OrderId");
        enqueue(createCallService(ServiceService.class).getReservationDetail(orderId), new RetrofitCallbackListener<ReservationPlaceResp>() {
            @Override
            public void onSuccess(Call<ReservationPlaceResp> call, ReservationPlaceResp result) {
                misLoading();
                if (result.getData() == null) return;
                meetOrderDetailBean = result.getData().transform();
                setUiData(meetOrderDetailBean);
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    @Override
    protected void doActivityBack() {
        if (isChangePayStatus) setResult(Constants.RESULT_OK_SIMPLE);
        finish();
    }

    @Override
    protected void setDetailData(ServeOrderDetailBean serveOrderDetailBean) {
        if (serveOrderDetailBean.getServeDetail() instanceof MeetOrderDetailBean) {
            frameServeDetail.removeAllViews();
            View view = getLayoutInflater().inflate(R.layout.meet_order_detail_body, null);
            frameServeDetail.addView(view);

            TextView txRoomName = ButterKnife.findById(view, R.id.tx_room_name);
            NoScrollListView listDate = ButterKnife.findById(view, R.id.list_date);
            SimpleDraweeView img_icon = ButterKnife.findById(view, R.id.img_icon);
            LinearLayout linear_sating = ButterKnife.findById(view, R.id.linear_sating);
            NoScrollGridView meet_facility_grid = ButterKnife.findById(view, R.id.meet_facility_grid);
            NoScrollGridView meet_service_grid = ButterKnife.findById(view, R.id.meet_service_grid);
            TextView tx_remark = ButterKnife.findById(view, R.id.tx_remark);
            TextView txNoneService = ButterKnife.findById(view, R.id.tx_none_service);
            TextView txNoneFacility = ButterKnife.findById(view, R.id.tx_none_facility);

            MeetOrderDetailBean meetOrderDetailBean = (MeetOrderDetailBean) serveOrderDetailBean.getServeDetail();
            List<MeetSessionItemBean> meetSessionItemBeanList = meetOrderDetailBean.getScheduleList();
            if (meetSessionItemBeanList == null || meetSessionItemBeanList.size() == 0) return;
            txRoomName.setText(meetOrderDetailBean.getMeetPlaceName());
            listDate.setAdapter(new ListCommonAdapter<MeetSessionItemBean>(this, meetSessionItemBeanList, R.layout.item_meet_sure_order) {
                @Override
                public void convert(ListCommonViewHolder helper, MeetSessionItemBean item, int position) {
                    helper.setText(R.id.tx_time, String.format("%s %s", item.getDate(), item.getSessionDurationTime()));
                    helper.setText(R.id.tx_pre_pay, item.getShowPriceUnitNumber());
                }
            });
            FrescoUtil.loadNetUrl(img_icon, meetOrderDetailBean.getMeetPic());
//            if (meetOrderDetailBean.getMatingList().size() == 0)
//                linear_sating.setVisibility(View.GONE);
//            else linear_sating.setVisibility(View.VISIBLE);

            initMating(meet_facility_grid, meet_service_grid);
            if (meetOrderDetailBean.getMatingList().size() == 0) {
                txNoneFacility.setVisibility(View.VISIBLE);
                meet_facility_grid.setVisibility(View.GONE);
            } else {
                txNoneFacility.setVisibility(View.GONE);
                meet_facility_grid.setVisibility(View.VISIBLE);
                meet_facility_grid.setAdapter(new ListCommonAdapter<String>(this, meetOrderDetailBean.getMatingList(), R.layout.meet_mating_item) {
                    @Override
                    public void convert(ListCommonViewHolder helper, String item, int position) {
                        helper.setText(R.id.tx_mating, item);
                    }
                });
            }
            if (meetOrderDetailBean.getServiceList().size() == 0) {
                txNoneService.setVisibility(View.VISIBLE);
                meet_service_grid.setVisibility(View.GONE);
            } else {
                txNoneService.setVisibility(View.GONE);
                meet_service_grid.setVisibility(View.VISIBLE);
                meet_service_grid.setAdapter(new ListCommonAdapter<String>(this, meetOrderDetailBean.getServiceList(), R.layout.meet_mating_item) {
                    @Override
                    public void convert(ListCommonViewHolder helper, String item, int position) {
                        helper.setText(R.id.tx_mating, item);
                    }
                });
                tx_remark.setText(String.format("备注信息：%s", meetOrderDetailBean.getRemark()));
            }
        }
    }

    /**
     * 初始化设备及服务内容的显示数目
     *
     * @param meetFacilityGrid 设备grid
     * @param meetServiceGrid  服务grid
     */
    private void initMating(NoScrollGridView meetFacilityGrid, NoScrollGridView meetServiceGrid) {
        if (TownApplicationLike.width > 750) {
            meetFacilityGrid.setNumColumns(4);
            meetServiceGrid.setNumColumns(4);
        } else {
            meetFacilityGrid.setNumColumns(3);
            meetServiceGrid.setNumColumns(3);
        }
    }

    @Override
    protected void cancelOrder() {
        showLoading();
        enqueue(createCallService(ServiceService.class).cancelMeetOrder(orderId), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                isChangePayStatus = true;
                misLoading();
                getOrderDetail();
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    @Override
    protected void payOrder() {
        PayInfo payInfo = (PayInfo) getIntent().getSerializableExtra("payInfo");
        if (payInfo == null || meetOrderDetailBean == null) return;
        Intent intent = new Intent(mContext, ChoosePayActivity.class);
        if (meetOrderDetailBean.getRedPacketMoney() != 0)
            payInfo.setRedPacketMoney(meetOrderDetailBean.getRedPacketMoney());
        payInfo.setPayType(meetOrderDetailBean.getChoosePayTypeList());
        payInfo.setOrderCreateClassfiy(2);//此字段用来表示是企业服务下单
        payInfo.setPaySubCode(Constants.MEET_CODE);//该字段用于余额支付，
        payInfo.setServerName("场地预订");
        payInfo.setOrderAllMoney(meetOrderDetailBean.getTotalMoney());
        payInfo.setThirdPayConfigType(Constants.PayType.Type_Meet);
        intent.putExtra("payInfo", payInfo);
        startActivity(intent);
    }
}
